package com.me.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.me.bean.AjaxResp;
import com.me.entity.GroupRemebers;
import com.me.entity.Groups;
import com.me.entity.NewsRecords;
import com.me.entity.RecentChat;
import com.me.entity.UserInfo;
import com.me.service.GroupChatMsgDataService;
import com.me.service.GroupService;
import com.me.service.RecentChatService;
import com.me.service.UserInfoService;
import com.me.util.GetTIme;
import com.me.util.JsonUtil;
import com.me.util.MapUtil;
import com.sun.javafx.applet.Splash;

@Controller
public class GroupController {

	@Autowired
	UserInfoService userInfoService;

	@Autowired
	GroupService groupService;
	
	@Autowired
	GroupChatMsgDataService groupChatMsgDataService;
	@Autowired
	RecentChatService recentChatService;

	@RequestMapping("/getRemebers")
	@ResponseBody
	public AjaxResp getMembersById(String group_id) {
		AjaxResp ar = new AjaxResp();
		ar.setSuccess(false);

		List result_list = groupService.getRemembersInfoByGroupId(group_id);
		String[] colNames = { "account", "username", "icon", "city" };
		List list = MapUtil.arrayToList(result_list.size(), 4, result_list, colNames);

		ar.setData(list);
		ar.setSuccess(true);
		return ar;
	}

	@RequestMapping("create_group")
	@ResponseBody
	@Transactional
	public AjaxResp create_group(String group_name, String remebers, String admin) {
		AjaxResp ar = new AjaxResp();
		ar.setSuccess(false);
		// 创建群
		Groups g = new Groups();
		g.setName(group_name);
		g.setUid(userInfoService.getUserIdByAccount(admin));
		g.setCreatetime(GetTIme.getNowTime());

		groupService.addGroup(g);

		Map<String, String> map = JsonUtil.fromJson(remebers, Map.class);
		for (int i = 0; i < map.size(); i++) {
			String account = map.get("account_" + i);
			int id = userInfoService.getUserIdByAccount(account);
			GroupRemebers gr=new GroupRemebers();
			gr.setGroup_id(g.getId());
			gr.setUid(id);
			groupService.addGroupRemebers(gr);
		}

		ar.setSuccess(true);
		ar.setData(g);
		
		return ar;
	}
	
	
	
	@ResponseBody
	@RequestMapping("add_remebers")
	@Transactional
	public AjaxResp add_remrbers(String group_id,String add_remebers,String senderAccount){
		AjaxResp ar=new AjaxResp();
		ar.setSuccess(false);
		String username="我邀请了：";
		//String adminUsername=((UserInfo)userInfoService.getUserByAccount(admin_account).get(0)).getUsername();
		Map<String,String> map=JsonUtil.fromJson(add_remebers, Map.class);
		for(int i=1;i<map.size();i++){
			String account=map.get("account_"+i);
			int id=userInfoService.getUserIdByAccount(account);
			GroupRemebers gr=new GroupRemebers();
			gr.setGroup_id(Integer.parseInt(group_id));
			gr.setUid(id);
			groupService.addGroupRemebers(gr);
			List user=userInfoService.getUserByAccount(account);
			if(user.isEmpty() || user.size()==0){
				continue;
			}
			username+=((UserInfo)user.get(0)).getUsername()+",";
			if(i==map.size()-1){
				username+="加入了群。";
			}
		}
		
		//向该群所有成员转发添加新成员消息
		groupChatMsgDataService.init(username, senderAccount, group_id,"json");
		
		
		
		ar.setSuccess(true);
		ar.setData(group_id);
		
		
		return ar;
	}
	
	
	@RequestMapping("delete_remeber_by_user")
	@ResponseBody
	@Transactional
	public AjaxResp deleteRemeber(String account,String group_id){
		AjaxResp ar=new AjaxResp();
		ar.setSuccess(false);
		int uid=userInfoService.getUserIdByAccount(account);
		List list=groupService.getRemebersEntityByUidAndGroupId(uid, Integer.parseInt(group_id));
		if(list.size()==0 || list.isEmpty()){
			return ar;
		}
		GroupRemebers g=(GroupRemebers)list.get(0);
		
		groupService.deleteRemebers(g);
		
		//删除最近联系人
		List list_=recentChatService.getRecent(uid,Integer.parseInt(group_id), "group");
		if(list_.size()==0 || list.isEmpty()){
			ar.setSuccess(true);
			return ar;
		}
		RecentChat rc=(RecentChat)list_.get(0);
		recentChatService.delete(rc);
		ar.setSuccess(true);
		
		
		
		
		return ar;
		
		
	}
}
